<template>
  <div>
    <el-card>
      <el-row type="flex" class="top" align="middle">
        <!-- 左列 -->
        <el-col>
          <el-button size="small " type="primary" @click="addCategoty">添加分类</el-button>
        </el-col>
        <!-- 右列 -->
        <el-col>
          <div style="margin-top: 15px;">
            <el-input v-model="searchInfo.title_contains" placeholder="请输入关键字" class="input-with-select search" @change="search">

              <el-button slot="append" icon="el-icon-search" />
            </el-input>
          </div>
        </el-col>
      <!-- 表格 -->
      </el-row>
      <hr>
      <el-row>
        <el-table
          height="350px"
          :data="categoryList"
          style="width: 100%;margin-top:20px"
        >
          <el-table-column
            align="center"
            prop="image"
            label="图片"
          >
            <template #default="{row}">

              <img v-fixImg="require('@/assets/image/1.jpg')" :src="row.image" alt="" class="pic">
            </template>

          </el-table-column>
          <el-table-column
            align="center"
            prop="title"
            label="标题"
            width="180"
          />
          <el-table-column
            align="center"
            prop="desc"
            label="介绍"
          />
          <el-table-column align="center" label="操作">
            <template #default="{row}">
              <el-button type="primary" size="small" @click="editCategory(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="delCategory(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination :total="total" background :current-page.sync="page" :page-size="searchInfo._limit" layout="prev, pager, next,jumper" @current-change="changePage" />
      </el-row>
    </el-card>
    <!-- 弹窗 -->
    <el-dialog
      :title="formData.id?'编辑':'添加'"
      :visible="isShowDialog"
      @close="btnCancel"
    >
      <el-form ref="edit" :model="formData" :rules="rules" label-width="80px">
        <el-form-item prop="title" label="标题">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item prop="desc" label="描述">
          <el-input v-model="formData.desc" />
        </el-form-item>
        <el-form-item label="封面" prop="image">
          <ImageUpload ref="cover" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import { getDetail, editCategory, addCategoty, delCategory, getSearchCount, getCategoryList } from '@/api/category'
export default {
  data() {
    return {
      categoryList: [],
      isShowDialog: false,
      formData: {
        id: '',
        title: '',
        desc: ''
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请添加描述', trigger: 'blur' }
        ]
      },
      searchInfo: {
        _limit: 5,
        _start: 0,
        title_contains: ''
      },
      total: 0,
      page: 1
    }
  },
  async created() {
    // 获取所有分类
    this.getCategoryList()
    // console.log(120, getSearchCount(''))
    const { data } = await getSearchCount('')
    this.total = data
  },
  methods: {
    // 获取数据
    async getCategoryList() {
      // this.total = await getSearchCount('')
      // this.categoryList = await getCategoryList(this.searchInfo)
      const { data } = await getCategoryList(this.searchInfo)
      this.categoryList = data
      console.log(130, this.categoryList)
    },
    async changePage(current) {
      // console.log(135, current)
      const { data } = await getCategoryList({ ...this.searchInfo, _start: current * this.searchInfo._limit - this.searchInfo._limit })
      // console.log(134, data)
      this.categoryList = data
    },

    // 编辑分类
    async editCategory(id) {
      // console.log(id)
      const { data } = await getDetail(id)
      this.formData = data
      // console.log(this.formData.image)
      if (this.formData.image) {
        this.$nextTick(() => {
          this.$refs.cover.fileList = [
            { url: this.formData.image }
          ]
        })
      }
      this.isShowDialog = true
    },

    // 添加分类
    addCategoty() {
      this.isShowDialog = true
    },

    // 弹窗确认按钮
    async btnOk() {
      try {
        // 校验表单
        await this.$refs.edit.validate()
        const fileList = this.$refs.cover.fileList
        if (fileList.length === 0) {
          this.$message.warning('请添加封面')
          return
        }
        // 发请求
        if (this.formData.id) {
        // 编辑

          if (fileList[0] && fileList[0].status !== 'success') {
            this.$message.error('请等图片上传完毕')
            return
          }
          console.log(130, this.formData)
          await editCategory({
            ...this.formData, image: fileList[0] ? fileList[0].url : ''
          })
        } else {
        // 新增
          if (fileList[0] && fileList[0].status !== 'success') {
            this.$message.error('请等图片上传完毕')
            return
          }
          await addCategoty({
            ...this.formData, image: fileList[0] ? fileList[0].url : ''
          })
          this.page = 1
        }
        // 关闭弹窗
        this.isShowDialog = false
        // 通知用户
        this.$message.success('操作成功')
        // 更新页面
        this.getCategoryList()
        const { data } = await getSearchCount('')
        this.total = data
      } catch (error) {
        console.log(error)
      }
    },

    // 弹窗取消按钮
    btnCancel() {
      // 清理数据
      this.formData = {
        id: '',
        title: '',
        desc: ''
      }
      this.$refs.cover.fileList = []
      // 清理校验结果
      this.$refs.edit.resetFields()
      // 关闭弹窗
      this.isShowDialog = false
    },

    // 删除分类
    async delCategory(id) {
      try {
        // 二次询问
        await this.$confirm('确定删除此分类吗?')
        // 发请求
        await delCategory(id)
        // 提示用户
        this.$message.success('操作成功')
        // 更新页面
        this.getCategoryList()
      } catch (error) {
        console.log(error)
      }
    },

    // 搜索
    async search() {
      // console.log(214, this.title_contains)
      // 获取搜索数量
      if (this.searchInfo.title_contains === '') {
        // this.total = 0
        this.searchInfo = {
          _limit: 5,
          _start: 0,
          title_contains: ''
        }
        console.log(123)
        this.getCategoryList()
        const { data } = await getSearchCount('')
        this.total = data
        return
      }
      // this.searchInfo._limit = await getSearchCount(this.searchInfo.title_contains)
      const { data } = await getSearchCount(this.searchInfo.title_contains)
      // 获取搜索列表
      this.total = data
      // console.log(250, data)
      console.log(256, this.searchInfo._limit)
      // this.total = this.searchInfo._limit
      // const {data} = await getCategoryList(this.searchInfo)
      this.getCategoryList(this.searchInfo)
      // console.log(224, res)
    }
  }
}
</script>

<style lang="scss" scoped>
  .pic{
  width: 180px;
  height: 80px;
  object-fit: contain;
  }

  ::v-deep .search{
    margin-bottom:10px ;

  }
</style>
